var sliderPlay = function (obj, option) {
    var slider = {
        option: {
            list: null,
            item: 'li',
            dot: null,
            interval: 1000,
            prevBtn: null,
            nextBtn: null,
            autoPlay: true,
            loadAfter: null, hover: null, animate: "translate", direction: "left"
        }, cur: 1,
        init: function (obj, opt) {
            this.option.list = opt['list'];
            this.option.item = opt['item'];
            var list = $(obj + " " + this.option.list);
            this.total = list.children().length;
            if (opt.animate)
                this.option.animate = opt.animate;
            if (opt.direction)
                this.option.direction = opt.direction;
            if (opt.dot)
                this.option.dot = obj + " " + opt.dot;
            if (opt.interval)
                this.option.interval = opt.interval
            if (opt.prevBtn)
                this.option.prevBtn =  opt.prevBtn;
            if (opt.nextBtn)
                this.option.nextBtn =  opt.nextBtn;
            if (typeof opt.autoPlay != undefined)
                this.option.autoPlay = opt.autoPlay
            if (typeof opt.loadAfter != undefined)
                this.option.loadAfter = opt.loadAfter
            if (typeof opt.hover != undefined)
                this.option.hover = opt.hover

            this.setUpAnimate(obj, list);
            this.bindUI(obj, list);
        },
        setUpAnimate: function (obj, list) {
            this.itemWidth = list.children(this.option.item).outerWidth();
            this.itemHeight = list.children(this.option.item).outerHeight();

            var w = this.itemWidth * this.total;
            var h = this.itemHeight * this.total;

            switch (this.option.animate) {
                case "fade"://淡入淡出效果
                    this.totalPage = Math.ceil(w / $(obj).width())
                    list.css({
                        "overflow": "hidden",
                        "position": "absolute",
                        "width": w, "height": this.itemHeight
                    })
                    list.find(this.option.item).eq(0).show();
                    list.find(this.option.item).eq(0).siblings(this.option.item).hide()
                    list.find(this.option.item).css({
                        "position": "absolute"
                    })
                    break;
                case "translate"://平移效果
                    this.translateWidth = $(obj).width()
                    this.translateHeight = this.itemHeight
                    list.css({
                        "overflow": "hidden",
                        "transition": "all 0.3s",
                        "backface-visibility": "hidden", "position": "absolute"
                    })

                    switch (this.option.direction) {
                        case "left":
                            this.totalPage = Math.ceil(w / $(obj).width())
                            list.find(this.option.item).css({
                                "float": "left"
                            })
                            list.css("width", w)
                            break;
                        case "right":
                            this.totalPage = Math.ceil(w / $(obj).width())
                            list.find(this.option.item).css({
                                "float": "right"
                            })
                            list.css("width", w)
                            break;
                        case "top":
                            this.totalPage = Math.round(h / $(obj).height())
                            list.find(this.option.item).css({
                                "float": "none"
                            })
                            list.css("height", h)
                            break;
                    }
                    break;
            }
        },
        bindUI: function (obj, list) {
            if (this.option.dot != null) {
                $(this.option.dot + " > li").click(function () {
                    slider.cur = parseInt($(this).find("a").text());
                    slider.setTranslate(list);
                })
            }

            if (this.option.prevBtn != null && this.option.nextBtn != null) {
                $(this.option.prevBtn).click(function () {
                    if (slider.cur <= 1)//没有上一页
                        return;
                    --slider.cur;
                    slider.setTranslate(list);
                })
                $(this.option.nextBtn).click(function () {
                    if (slider.cur >= slider.totalPage)//没有下一页
                        return;
                    ++slider.cur;
                    slider.setTranslate(list);
                })
            }

            this.setTranslate(list);
            if (this.option.autoPlay)
                this.autoPlay(obj, list);
        },
        autoPlay: function (obj, list) {
            function timer() {
                if (slider.cur == slider.totalPage) {
                    slider.cur = 1;
                    slider.setTranslate(list);
                    return;
                }
                ++slider.cur;
                slider.setTranslate(list);
            }

            var autoPlay = setInterval(timer, 1000)
            $(obj).hover(function () {
                if (slider.option.hover != null && typeof slider.option.hover.after == "function") {
                    slider.option.hover.after()
                }
                clearInterval(autoPlay)
            }, function () {
                if (slider.option.hover != null && typeof slider.option.hover.before == "function") {
                    slider.option.hover.before()
                }
                autoPlay = setInterval(timer, 1000)
            })
        },
        setDot: function () {
            if (this.option.dot != null) {
                $(this.option.dot).find("li").siblings("li").removeClass("action")
                $(this.option.dot).find("li").eq(this.cur - 1).addClass("action")
            }
        },
        setTranslate: function (list) {
            switch (this.option.animate) {
                case "fade":
                    list.find(this.option.item).eq(this.cur - 1).siblings(this.option.item).fadeOut(1000)
                    list.find(this.option.item).eq(this.cur - 1).fadeIn(1000);
                    break;
                case "translate":
                    switch (this.option.direction) {
                        case "left":
                            this.distance = (this.cur - 1) * this.translateWidth;
                            list.css("transform", 'translate3d(-' + this.distance + 'px, 0px, 0px)')
                            break;
                        case "right":
                            this.distance = (this.totalPage * this.translateWidth) - (this.cur * this.translateWidth);
                            list.css("transform", 'translate3d(-' + this.distance + 'px, 0px, 0px)')
                            break;
                        case "top":
                            this.distance = (this.cur - 1) * this.translateHeight;
                            list.css("transform", 'translate3d(0px,-' + this.distance + 'px, 0px)')
                            break;
                    }
                    break;
            }
            this.setDot()
            if (typeof this.option.loadAfter == "function")
                this.option.loadAfter(this.cur,this.totalPage);
        }
    }
    slider.init(obj, option)
}